%> @file cc_cell_tf2ss.m
%> @brief Converts the Continuous-time Transfer Matrix to the Discrete State Space form.
%>
%> @author Mikhail Konnik
%> @date   10 January 2012
%>
%> @section celltf2ss Transfer matrix to State space conversion
%> A transfer function (also known as the system function[1] or network function) is a mathematical representation, in terms of spatial or temporal frequency, of the relation between the input and output of a linear time-invariant system. With optical imaging devices, for example, it is the Fourier transform of the point spread function (hence a function of spatial frequency) i.e. the intensity distribution caused by a point object in the field of view.
%>
%> Construct numerator of the transfer function. This means that if in the numerator the function is \f$ s^2 + 2s + 3 \f$ then the numerator for the MATLAB will be num = [1 2 3].
%> num = [500 8750 53271 151200 18000];
%>
%> Construct denumerator of the transfer function. This means that if in the numerator the function is \f$ s^3 + 4s^2 + 3 \f$ then the numerator for the MATLAB will be num = [1 4 0 3]. The numerator and denumerator may be of different
%> denum = [500 8750 54000 135000 108000];
%======================================================================
%> @param nominator		= cell array of numerators of the Transfer Matrix (continuous-time).
%> @param denominator		= cell array of denominators of the Transfer Matrix (continuous-time).
%> @param T_f			= sampling time for the discretization.
%> @retval A			= discrete state evolution matrix A.
%> @retval B			= discrete input matrix B.
%> @retval C			= discrete output matrix C.
%> @retval D			= discrete feedthrough matrix D.
% ======================================================================
function [A,B,C,D] = cc_cell_tf2ss(nominator,denominator,T_f)

[row_cc_atm_nomin,col_cc_atm_nomin] = size(nominator);

for ii=1:row_cc_atm_nomin
	for jj=1:col_cc_atm_nomin

		if isempty(nominator{ii,jj});
		nominator{ii,jj} = 0; %%% if the cell for the nominator of actuator's TF is empty - substitute zero.
		end

		if isempty(denominator{ii,jj});
		denominator{ii,jj} = 1; %%% if the cell for the nominator of actuator's TF is empty - substitute zero.
		end

	end %% for jj=1:col_cc_atm_nomin
end %%% for ii=1:row_cc_atm_nomin



%%%%%%%%%%%%%%%%%%%%%%% #### Converting Transfer matrix to State Space %%%%%%
transfer_matrix = tf(nominator, denominator);

transfer_matrix_minrealiz = ss(transfer_matrix,'min'); %%% obtain minimal realisation of the plant.

[A,B,C,D] = ssdata(transfer_matrix_minrealiz); %%% continuous State Space plant

[A,B,C,D] = c2dm(A,B,C,D,T_f,'zoh');  %% discretization of the atmosphere disturbance for the controller usage
